import { defineConfig, loadEnv } from 'vite';
import path from 'path'
import vue from '@vitejs/plugin-vue';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { createHtmlPlugin } from 'vite-plugin-html'
import viteCompression from 'vite-plugin-compression'

const getTarget = (mode: string, target: string) => {
	return loadEnv(mode, process.cwd())[target];
};

export default ({ mode }) => defineConfig({
	resolve: {
		alias: {
			// 设置路径
			'~': path.resolve(__dirname, './'),
			// 设置别名
			'@': path.resolve(__dirname, './src')
		},
		extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
	},
	base: './',
	server: {
		host: '0.0.0.0',
		port: 5174,
		open: true
	},
	plugins: [
		vue(),
		VueSetupExtend(),
		AutoImport({
			imports: ['vue', 'vue-router'],
			// 可选，用于自动导入组件类型
			dts: 'components.d.ts'
		}),
		viteCompression(),
		Components({
			resolvers: [ElementPlusResolver()]
		}),
		// 添加以下6行，并根据自己需求进行配置
		createSvgIconsPlugin({
			// 指定需要缓存的图标文件夹
			iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
			// 指定symbolId格式
			symbolId: '[name]',
		}),
		createHtmlPlugin({
			inject: {
				data: {
					//获取标题变量
					title: getTarget(mode, "VITE_APP_TITLE")
				},
			},
		})
	],
	optimizeDeps: {
		include: ['schart.js']
	}
});
